package ranab.jar;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.zip.ZipFile;
import java.util.zip.ZipEntry;
import java.util.jar.JarFile;


/**
 * This is JAR file extractor class. It extracts the JAR file
 * in a separate thread. Where we are passing a MyJarObserver
 * object to track the current status of this decompression.
 *
 * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
 */
public
class JarSelectiveExtractor extends JarExtractor {

    private ZipEntry mEntry[];
    
    public JarSelectiveExtractor(ZipEntry entry[], File jarFile, File dir) {
        super(jarFile, dir);
        mEntry = entry;
    }
    
    /**
     * invoke a new thread and start decompression
     */
    public void extract() {
        Thread th = new Thread(this);
        th.start();
    }

    public void run() {

        mbIsPauseRequest = false;
        mbIsStopRequest = false;

        mObserverCont.start();
        try {
            ZipFile jf = new JarFile(mJarFile);
            mObserverCont.setCount(mEntry.length);

            for(int i=0; i<mEntry.length; i++) {

                // check request
                while (mbIsPauseRequest && (!mbIsStopRequest) ) {
                    Thread.sleep(100);
                }

                if (mbIsStopRequest) {
                    return;
                }
                extract(jf, mEntry[i]);
                mObserverCont.setNext(mEntry[i]);
            }
        } catch (Exception ex) {
            mObserverCont.setError(ex.getMessage());
        } finally {
            mbIsStopRequest = true;
            mObserverCont.end();
        }
    }

    /**
     * get jar file name
     */
    public String toString() {
        return mJarFile.getAbsolutePath();
    }

}